package org.x.log.dubbo;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.scheduling.annotation.Async;
import org.x.api.system.dto.LoginLogDTO;
import org.x.api.system.dto.OperateLogDTO;
import org.x.api.system.dto.SystemLogDTO;
import org.x.api.system.feign.RemoteLogService;
import org.x.common.base.result.Result;
import org.x.log.es.SysLoginLogES;
import org.x.log.es.SysOperateLogES;
import org.x.log.es.SysSystemLogES;

import java.util.UUID;

@DubboService(version = "1.0.0", group = "x-log")
public class RemoteLogServiceImpl implements RemoteLogService {

    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    @Override
    public Result<Boolean> recordLoginLog(LoginLogDTO logDTO) {
        SysLoginLogES log = new SysLoginLogES();
        BeanUtils.copyProperties(logDTO, log);
        log.setId(UUID.randomUUID().toString());
        elasticsearchOperations.save(log);
        return Result.success(true);
    }

    @Override
    @Async
    public Result<Boolean> recordOperateLog(OperateLogDTO logDTO) {
        SysOperateLogES log = new SysOperateLogES();
        BeanUtils.copyProperties(logDTO, log);
        log.setId(UUID.randomUUID().toString());
        elasticsearchOperations.save(log);
        return Result.success(true);
    }

    @Override
    @Async
    public Result<Boolean> recordSystemLog(SystemLogDTO logDTO) {
        SysSystemLogES log = new SysSystemLogES();
        BeanUtils.copyProperties(logDTO, log);
        log.setId(UUID.randomUUID().toString());
        elasticsearchOperations.save(log);
        return Result.success(true);
    }
}